package com.woniu.cloud.shopassembly.apiImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.cloud.api.GoodApi;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.entity.TGood;
import com.woniu.cloud.form.GoodInsertForm;
import com.woniu.cloud.form.GoodUpdateForm;
import com.woniu.cloud.shopassembly.service.ITGoodService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 平台对店铺商品表 前端控制器
 * </p>
 *
 * @author hyy
 * @since 2021-09-22
 */
@RestController
@Slf4j
public class GoodApiImpl implements GoodApi {

    @Resource
    private ITGoodService goodService;

    @Override
    @GetMapping("getGoodByGoodId")
    @GlobalTransactional
    public Result getGoodByGoodId(Integer goodId) {
        log.info("通过Id查找商品->goodId:{}",goodId);
        if (null==goodId) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("开始查询->goodId:{}",goodId);
            TGood good = goodService.getGoodByGoodId(goodId);
            if (null!=good) {
                log.info("查询结果->{}",good);
                return Result.success("该商品存在",good);
            }
            return Result.success("该商品不存在",null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }
    }

    @Override
    @GetMapping("getGoodByGoodName")
    @GlobalTransactional
    public Result getGoodByGoodName(String goodName,Integer pageIndex) {
        log.info("通过商品名称查找商品->goodName:{},pageIndex:{}",goodName,pageIndex);
        if (null==goodName) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("开始查询->goodName:{},pageIndex:{}",goodName,pageIndex);
            return goodService.getGoodByGoodName(goodName,pageIndex);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }

    }

    @Override
    @GetMapping("getGoodByGoodType")
    @GlobalTransactional
    public Result getGoodByGoodType(String goodType,Integer pageIndex) {
        log.info("通过商品类型查找商品->goodType:{},pageIndex:{}",goodType,pageIndex);
        if (null==goodType) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("开始查询->goodType:{},pageIndex:{}",goodType,pageIndex);
            return goodService.getGoodByGoodType(goodType,pageIndex);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }
    }

    @Override
    @GetMapping("getGoodTypeList")
    @GlobalTransactional
    public Result getGoodTypeList() {
        log.info("商品类型集合->");
        try {
            log.info("开始查询->");
            return goodService.getGoodTypeList();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }
    }

    @Override
    @PostMapping("insertGood")
    @GlobalTransactional
    public Result insertGood(@RequestBody GoodInsertForm goodForm) {
        log.info("商品添加->goodForm:{}",goodForm);
        try {
            log.info("开始添加->goodForm:{}",goodForm);
            TGood good = new TGood();
            BeanUtils.copyProperties(goodForm, good);
            return goodService.insertGood(good);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",false);
        }
    }

    @Override
    @PutMapping("updateGoodByGoodId")
    @GlobalTransactional
    public Result updateGoodByGoodId(@RequestBody GoodUpdateForm goodForm) {
        log.info("商品信息修改->goodForm:{}",goodForm);
        try {
            log.info("开始修改->goodForm:{}",goodForm);
            TGood good = new TGood();
            BeanUtils.copyProperties(goodForm, good);
            return goodService.updateGoodByGoodId(good);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",false);
        }
    }

    @Override
    @PutMapping("updateGoodNumByGoodId")
    @GlobalTransactional
    public Result updateGoodNumByGoodId(Integer goodId, Integer goodNum) {
        log.info("商品库存修改->goodId:{},goodNum:{}",goodId,goodNum);
        try {
            log.info("开始修改->goodId:{},goodNum:{}",goodId,goodNum);
            return goodService.updateGoodNumByGoodId(goodId,goodNum);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",false);
        }
    }

    @Override
    @PutMapping("updateGoodSellNumByGoodId")
    @GlobalTransactional
    public Result updateGoodSellNumByGoodId(Integer goodId, Integer goodSellNum) {
        log.info("商品销量修改->goodId:{},goodSellNum:{}",goodId,goodSellNum);
        try {
            log.info("开始修改->goodId:{},goodSellNum:{}",goodId,goodSellNum);
            return goodService.updateGoodSellNumByGoodId(goodId,goodSellNum);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",false);
        }
    }

    @Override
    @DeleteMapping("deleteGoodByGoodId")
    @GlobalTransactional
    public Result deleteGoodByGoodId(Integer goodId) {
        log.info("根据商品id删除商品->goodId:{}",goodId);
        try {
            log.info("开始删除->goodId:{}",goodId);
            return goodService.deleteGoodByGoodId(goodId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",false);
        }
    }
}
